package com.mc2.docapp.disabilityassessmenttool;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.InputFilter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.mc2.docapp.disabilityassessmenttool.Misc.InputFilterMinMax;
import com.mc2.docapp.disabilityassessmenttool.UpperLimb.C_active_rom;
import com.mc2.docapp.disabilityassessmenttool.UpperLimb.C_ulca_elbow;
import com.mc2.docapp.disabilityassessmenttool.UpperLimb.C_ulca_shoulder;
import com.mc2.docapp.disabilityassessmenttool.UpperLimb.C_ulca_wrist;
import com.mc2.docapp.disabilityassessmenttool.UpperLimb.CustDataType;
import com.mc2.docapp.disabilityassessmenttool.UpperLimb.InnerCalculations;
import com.mc2.docapp.disabilityassessmenttool.utils.MailerObject;
import com.mc2.docapp.disabilityassessmenttool.utils.SendMailTLS;

import java.util.HashSet;

import io.realm.Realm;
import io.realm.RealmConfiguration;

import static com.mc2.docapp.disabilityassessmenttool.utils.Statics.getEditTexts;
import static com.mc2.docapp.disabilityassessmenttool.utils.Statics.router;

public class UpLimbAcRom extends AppCompatActivity {

    //this is act identifier
    private static final int id = Activities.UPPER_LIMB_AC_ROM;

    //creating new sub-component
    public C_ulca_shoulder c_ulca_shoulder = new C_ulca_shoulder();
    public C_ulca_elbow c_ulca_elbow = new C_ulca_elbow();
    public C_ulca_wrist c_ulca_wrist = new C_ulca_wrist();

    private EditText shoulderFlexionExtensionArcLeft;
    private EditText shoulderFlexionExtensionArcRight;
    private EditText shoulderInternalExternalRotationArcLeft;
    private EditText shoulderInternalExternalRotationArcRight;
    private EditText sholderAdductionAbductionArcLeft;
    private EditText wristRadialUlnarDeviationArcRight;
    private EditText wristRadialUlnarDeviationArcLeft;
    private EditText wristFlexionExtensionArcRight;
    private EditText sholderAdductionAbductionArcRight;
    private EditText elbowFlexionExtensionArcLeft;
    private EditText elbowFlexionExtensionArcRight;
    private EditText elbowSupinationPronationArcLeft;
    private EditText elbowSupinationPronationArcRight;
    private EditText wristFlexionExtensionArcLeft;

    //db instance
    private Realm realm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_up_limb_ac_rom);

        //
        shoulderFlexionExtensionArcLeft = findViewById(R.id.shoulderFlexionExtensionArcLeft);

        shoulderFlexionExtensionArcRight = findViewById(R.id.shoulderFlexionExtensionArcRight);

        shoulderInternalExternalRotationArcLeft = findViewById(R.id.shoulderInternalExternalRotationArcLeft);

        shoulderInternalExternalRotationArcRight = findViewById(R.id.shoulderInternalExternalRotationArcRight);

        sholderAdductionAbductionArcLeft = findViewById(R.id.sholderAdductionAbductionArcLeft);

        sholderAdductionAbductionArcRight = findViewById(R.id.sholderAdductionAbductionArcRight);

        elbowFlexionExtensionArcLeft = findViewById(R.id.elbowFlexionExtensionArcLeft);

        elbowFlexionExtensionArcRight = findViewById(R.id.elbowFlexionExtensionArcRight);

        elbowSupinationPronationArcLeft = findViewById(R.id.elbowSupinationPronationArcLeft);

        elbowSupinationPronationArcRight = findViewById(R.id.elbowSupinationPronationArcRight);

        wristFlexionExtensionArcLeft = findViewById(R.id.wristFlexionExtensionArcLeft);

        wristFlexionExtensionArcRight = findViewById(R.id.wristFlexionExtensionArcRight);

        wristRadialUlnarDeviationArcLeft = findViewById(R.id.wristRadialUlnarDeviationArcLeft);

        wristRadialUlnarDeviationArcRight = findViewById(R.id.wristRadialUlnarDeviationArcRight);

//        Button saveNnextButton = findViewById(R.id.saveAndNext);
        Button saveNnextButton = findViewById(R.id.saveAndNextUplImb);

        initRealmDB();
        try {
            //mapUI(new C_active_rom().strip((realm.where(C_active_rom.class)
            //    /*.equalTo("id", id)*/.findAll()).get(0)));
            System.out.println();
        } catch (Exception e) {
            System.out.println(e.getCause() + " " + e.getMessage());
        }

        Button clearAll = (Button) findViewById(R.id.clear);
        clearAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HashSet<EditText> views = new HashSet<>();
                getEditTexts((ViewGroup) findViewById(R.id.activity_main), views);
                for (EditText h : views)
                    h.setText("");
            }
        });



        saveNnextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                c_ulca_shoulder.setAdduction_abduction_arc(new CustDataType(Integer.parseInt(IfNull(sholderAdductionAbductionArcRight.getText().toString(), "180")),
                        Integer.parseInt(IfNull(sholderAdductionAbductionArcLeft.getText().toString(), "180")), 180d, 0, 0));
                c_ulca_shoulder.setFlexion_extension_arc(new CustDataType(Integer.parseInt(IfNull(shoulderFlexionExtensionArcRight.getText().toString(), "220")),
                        Integer.parseInt(IfNull(shoulderFlexionExtensionArcLeft.getText().toString(), "220")), 220d, 0, 0));
                c_ulca_shoulder.setInternal_external_rotation_arc(new CustDataType(Integer.parseInt(IfNull(shoulderInternalExternalRotationArcRight.getText().toString(), "180")),
                        Integer.parseInt(IfNull(shoulderInternalExternalRotationArcLeft.getText().toString(), "180")), 180d, 0, 0));

                c_ulca_shoulder.calc(c_ulca_shoulder);


                c_ulca_elbow.setFlexion_extension_arc(new CustDataType(Integer.parseInt(IfNull(elbowFlexionExtensionArcRight.getText().toString(), "150")),
                        Integer.parseInt(IfNull(elbowFlexionExtensionArcLeft.getText().toString(), "150")), 150d, 0, 0));
                c_ulca_elbow.setSupination_pronation_arc(new CustDataType(Integer.parseInt(IfNull(elbowSupinationPronationArcRight.getText().toString(), "180")),
                        Integer.parseInt(IfNull(elbowSupinationPronationArcLeft.getText().toString(), "180")), 180d, 0, 0));

                c_ulca_elbow.calc(c_ulca_elbow);

                c_ulca_wrist.setFlexion_extension_arc(new CustDataType(Integer.parseInt(IfNull(wristFlexionExtensionArcRight.getText().toString(), "160")),
                        Integer.parseInt(IfNull(wristFlexionExtensionArcLeft.getText().toString(), "160")), 160d, 0, 0));
                c_ulca_wrist.setRadial_ulnar_deviation_arc(new CustDataType(Integer.parseInt(IfNull(wristRadialUlnarDeviationArcRight.getText().toString(), "55")),
                        Integer.parseInt(IfNull(wristRadialUlnarDeviationArcLeft.getText().toString(), "55")), 55d, 0, 0));

                c_ulca_wrist.calc(c_ulca_wrist);

                C_active_rom c_active_rom = new C_active_rom();
                c_active_rom.setUlca_shoulder(c_ulca_shoulder);
                c_active_rom.setUlca_elbow(c_ulca_elbow);
                c_active_rom.setUlca_wrist(c_ulca_wrist);
                //
                InnerCalculations in = new InnerCalculations();
                in.setSummation_left(c_active_rom.getUlca_shoulder().getInner_calculations().getSummation_left() +
                        c_active_rom.getUlca_elbow().getInner_calculations().getSummation_left() +
                        c_active_rom.getUlca_wrist().getInner_calculations().getSummation_left());

                in.setSummation_right(c_active_rom.getUlca_shoulder().getInner_calculations().getSummation_right() +
                        c_active_rom.getUlca_elbow().getInner_calculations().getSummation_right() +
                        c_active_rom.getUlca_wrist().getInner_calculations().getSummation_right());

                c_active_rom.setInnerCalculations(in);
                System.out.println();
                c_active_rom.setId(id);
                NewPatient.finalDto.getClass_upperLimb_component().getClass_arm_component().setActive_rom_degrees(c_active_rom);

                realm.beginTransaction();
                realm.delete(C_active_rom.class);
                realm.insert(c_active_rom);
                realm.commitTransaction();
                try {
                    String str = new ObjectMapper().writeValueAsString(c_active_rom).toString();
//                    SendMailTLS.getMailer().sendMail(new MailerObject("saurab.hbhumkar@gmail.com", "MC2 Docapp", "str"));
                } catch (Exception e) {
                    System.out.println();
                }
                //
                Intent intent = new Intent(UpLimbAcRom.this, router(UpLimbAcRom.class));
                startActivity(intent);
            }
        });


        /*Set Limit to Inputs*/
        sanitizeInput();

    }

    public String IfNull(String value, String defaultValue) {
        if (value == null || value.isEmpty()) {
            return defaultValue;
        } else {
            return value;
        }
    }


    private void mapUI(C_active_rom oldData) {
        shoulderFlexionExtensionArcLeft.setText(String.valueOf((long) (oldData.getUlca_shoulder().getFlexion_extension_arc().getActual_left())));
        shoulderFlexionExtensionArcRight.setText(String.valueOf((long) (oldData.getUlca_shoulder().getFlexion_extension_arc().getActual_right())));

        shoulderInternalExternalRotationArcLeft.setText(String.valueOf((long) (oldData.getUlca_shoulder().getInternal_external_rotation_arc().getActual_left())));
        shoulderInternalExternalRotationArcRight.setText(String.valueOf((long) (oldData.getUlca_shoulder().getInternal_external_rotation_arc().getActual_right())));

        sholderAdductionAbductionArcLeft.setText(String.valueOf((long) (oldData.getUlca_shoulder().getAdduction_abduction_arc().getActual_left())));
        sholderAdductionAbductionArcRight.setText(String.valueOf((long) (oldData.getUlca_shoulder().getAdduction_abduction_arc().getActual_right())));

        elbowFlexionExtensionArcLeft.setText(String.valueOf((long) (oldData.getUlca_elbow().getFlexion_extension_arc().getActual_left())));
        elbowFlexionExtensionArcRight.setText(String.valueOf((long) (oldData.getUlca_elbow().getFlexion_extension_arc().getActual_right())));

        elbowSupinationPronationArcLeft.setText(String.valueOf((long) (oldData.getUlca_elbow().getSupination_pronation_arc().getActual_left())));
        elbowSupinationPronationArcRight.setText(String.valueOf((long) (oldData.getUlca_elbow().getSupination_pronation_arc().getActual_right())));

        wristFlexionExtensionArcLeft.setText(String.valueOf((long) (oldData.getUlca_wrist().getFlexion_extension_arc().getActual_left())));
        wristFlexionExtensionArcRight.setText(String.valueOf((long) (oldData.getUlca_wrist().getFlexion_extension_arc().getActual_right())));

        wristRadialUlnarDeviationArcLeft.setText(String.valueOf((long) (oldData.getUlca_wrist().getRadial_ulnar_deviation_arc().getActual_left())));
        wristRadialUlnarDeviationArcRight.setText(String.valueOf((long) (oldData.getUlca_wrist().getRadial_ulnar_deviation_arc().getActual_right())));

    }


    private void initRealmDB() {
        Realm.init(getApplicationContext());
        RealmConfiguration config = new RealmConfiguration.Builder()
                .name(id + ".realm")
                .build();
        realm = Realm.getInstance(config);
    }

    public void sanitizeInput() {
        shoulderFlexionExtensionArcLeft.setFilters(new InputFilter[]{new InputFilterMinMax("0", "220")});
        shoulderFlexionExtensionArcRight.setFilters(new InputFilter[]{new InputFilterMinMax("0", "220")});

        shoulderInternalExternalRotationArcLeft.setFilters(new InputFilter[]{new InputFilterMinMax("0", "180")});
        shoulderInternalExternalRotationArcRight.setFilters(new InputFilter[]{new InputFilterMinMax("0", "180")});

        sholderAdductionAbductionArcLeft.setFilters(new InputFilter[]{new InputFilterMinMax("0", "180")});
        sholderAdductionAbductionArcRight.setFilters(new InputFilter[]{new InputFilterMinMax("0", "180")});

        elbowFlexionExtensionArcLeft.setFilters(new InputFilter[]{new InputFilterMinMax("0", "150")});
        elbowFlexionExtensionArcRight.setFilters(new InputFilter[]{new InputFilterMinMax("0", "150")});

        elbowSupinationPronationArcLeft.setFilters(new InputFilter[]{new InputFilterMinMax("0", "180")});
        elbowSupinationPronationArcRight.setFilters(new InputFilter[]{new InputFilterMinMax("0", "180")});

        wristFlexionExtensionArcLeft.setFilters(new InputFilter[]{new InputFilterMinMax("0", "160")});
        wristFlexionExtensionArcRight.setFilters(new InputFilter[]{new InputFilterMinMax("0", "160")});

        wristRadialUlnarDeviationArcLeft.setFilters(new InputFilter[]{new InputFilterMinMax("0", "55")});
        wristRadialUlnarDeviationArcRight.setFilters(new InputFilter[]{new InputFilterMinMax("0", "55")});

    }

}
